package com.weimob.mcloud.wechatwork.customer.export;

import com.weimob.mcloud.wechatwork.customer.facade.impl.process.CustomerStageRelationFacade;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BizException;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.process.CurrentProcessListBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.process.ProcessStatisticBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.process.StageCustomerListBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.process.StageRelationDetailBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.process.StageAuthDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.process.StageSetUpDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.process.StageStatisticDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.service.CustomerStageRelationExport;
import com.weimob.mcloud.wechatwork.customer.service.user.utils.SoaResponseUtil;
import com.weimob.soa.common.response.SoaResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;

import static com.weimob.mcloud.wechatwork.customer.service.user.constant.RedisKeyConstant.CUSTOMER_PROCESS_OPERATION;
import static com.weimob.mcloud.wechatwork.customer.service.user.exception.BaseErrorCode.PROCESS_STAGE_OPERATION_CONFLICT;

/**
 * @author yuanhao
 * @date 2022/9/13 11:59
 */
@Slf4j
@Service
public class CustomerStageRelationExportImpl implements CustomerStageRelationExport {

    @Autowired
    private CustomerStageRelationFacade customerStageRelationFacade;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public SoaResponse<List<CurrentProcessListBO>, ?> currentProcessList(StageAuthDTO stageAuthDTO) {
        return SoaResponseUtil.ok(customerStageRelationFacade.currentProcessList(stageAuthDTO));
    }

    @Override
    public SoaResponse<Boolean, ?> stageSetUp(StageSetUpDTO stageSetUpDTO) {
        //判断此客户+流程+阶段是否在操作 在操作则提示
        checkIsOperation(stageSetUpDTO);
        Boolean res = Boolean.FALSE;
        try {
            res = customerStageRelationFacade.stageSetUp(stageSetUpDTO);
        } finally {
            stringRedisTemplate.delete(generalKey(stageSetUpDTO));
        }
        return SoaResponseUtil.ok(res);
    }

    /**
     * 判断此客户+流程+阶段是否在操作 在操作则提示
     *
     * @param stageSetUpDTO stageSetUpDTO
     */
    private void checkIsOperation(StageSetUpDTO stageSetUpDTO) {
        String key = generalKey(stageSetUpDTO);
        if (Boolean.FALSE.equals(stringRedisTemplate.opsForValue().setIfAbsent(key, "", 10, TimeUnit.SECONDS))) {
            throw new BizException(PROCESS_STAGE_OPERATION_CONFLICT);
        }
    }

    private String generalKey(StageSetUpDTO stageSetUpDTO) {
        StringJoiner stringJoiner = new StringJoiner(":");
        stringJoiner.add(CUSTOMER_PROCESS_OPERATION);
        stringJoiner.add(stageSetUpDTO.getProcessId());
        //新增阶段没有当前阶段id
        if(StringUtils.isBlank(stageSetUpDTO.getCurrentStageId())){
            stringJoiner.add(stageSetUpDTO.getTargetStageId());
        }else {
            stringJoiner.add(stageSetUpDTO.getCurrentStageId());
        }
        stringJoiner.add(stageSetUpDTO.getCustomerId());
        return stringJoiner.toString();
    }

    @Override
    public SoaResponse<Boolean, ?> isAuth(StageAuthDTO authDTO) {
        return SoaResponseUtil.ok(customerStageRelationFacade.isAuth(authDTO));
    }

    @Override
    public SoaResponse<List<StageRelationDetailBO>, ?> currentStageList(StageAuthDTO stageAuthDTO) {
        return SoaResponseUtil.ok(customerStageRelationFacade.currentStageList(stageAuthDTO));
    }

    @Override
    public SoaResponse<List<StageCustomerListBO>, ?> fetchStageCustomerList(StageAuthDTO stageAuthDTO) {
        return SoaResponseUtil.ok(customerStageRelationFacade.fetchStageCustomerList(stageAuthDTO));
    }

    @Override
    public SoaResponse<List<ProcessStatisticBO>, ?> fetchStatisticList(StageStatisticDTO stageStatisticDTO) {
        return SoaResponseUtil.ok(customerStageRelationFacade.fetchStatisticList(stageStatisticDTO));
    }

    @Override
    public SoaResponse<StageRelationDetailBO, ?> stageRelationDetail(StageAuthDTO stageAuthDTO) {
        return SoaResponseUtil.ok(customerStageRelationFacade.stageRelationDetail(stageAuthDTO));
    }
}
